package jt.classic.system.workflow;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.limp.basework.SimpleBean;
import org.limp.basework.console.XSystemConfig;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.HTMLTool;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 流转web服务
 *
 * @author Bizkit
 *
 */
public class WorkflowServiceAction extends DispatchAction {
    /**
     * 流程绘画服务
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward drawing(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        CommonBean4HSQ cb = new CommonBean4HSQ(request);
        Map param = cb.getResource();

        String name = (String) param.get("name");
        String xml = (String) param.get("xml");

        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);

            manager.synFromFlowDrawing(name, xml);

            conn.commit();

            request.setAttribute("result", "1");
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }
}
